package Utils;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.ProtocolException;

import javax.net.ssl.*;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;

public class HttpConnect {
    public static String HttpGet_OkHttp(String url, Map<String, String> header_map)  {


        OkHttpClient client = new OkHttpClient().newBuilder()
                .sslSocketFactory(createSSLSocketFactory(), new TrustAllCerts())
                .hostnameVerifier(new TrustAllHostnameVerifier())
                .build();
        Request.Builder builder = new Request.Builder();
        if (header_map != null){
            for (Map.Entry<String, String> map : header_map.entrySet()) {
                builder.addHeader(map.getKey(), map.getValue());
            }
        }

        Request request = builder.url(url).get().build();

        int call_time = 0;
        while (call_time <= 5) {
            try{

                Response response = client.newCall(request).execute();
                String str = response.body().string();
                return str;
            } catch (IOException e) {
                e.printStackTrace();
                call_time++;
            }
        }
        return null;
    }

    public static String HttpPost_OkHttp(String url, Map<String, String> header_map, Map<String, String> data_map) {

        OkHttpClient client = new OkHttpClient().newBuilder()
                .sslSocketFactory(createSSLSocketFactory(), new TrustAllCerts())
                .hostnameVerifier(new TrustAllHostnameVerifier())
                .build();

        FormBody.Builder form_builder = new FormBody.Builder();
        for (Map.Entry<String, String> map : data_map.entrySet()) {
            form_builder.add(map.getKey(), map.getValue());
        }
        FormBody formBody = form_builder.build();

        Request.Builder builder = new Request.Builder();
        for (Map.Entry<String, String> map : header_map.entrySet()) {
            builder.addHeader(map.getKey(), map.getValue());
        }
        Request request = builder.url(url).post(formBody).build();



        int call_time = 0;
        while (call_time <= 5) {
            try{
                Response response = client.newCall(request).execute();
                String str = response.body().string();
                return str;
            } catch (IOException e) {
                e.printStackTrace();
                call_time++;
            }
        }
        return null;
    }


    private static class TrustAllCerts implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}

        @Override
        public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}
    }

    private static class TrustAllHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    private static SSLSocketFactory createSSLSocketFactory() {
        SSLSocketFactory ssfFactory = null;
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[] { new TrustAllCerts() }, new SecureRandom());
            ssfFactory = sc.getSocketFactory();
        } catch (Exception e) {
        }

        return ssfFactory;
    }
}

